Masthead

Formatting Code

1. Introduction

There is a lot of code out there that is written in a what that is really challenging to debug, maintain and expand. The code below is one such example:

List=[Row[0] for Row in arcpy.da.SearchCursor(TheShapefile, "County")]

The line of code above will get all the values from the attibute "County" in the specific shapefile and put them in a list. The code below does exactly the same thing:

TheRows=arcpy.SearchCursor(TheShapefile)

List=[]
for TheRow in TheRows:
	List.append(TheRow.getValue("County"))

The two blocks of code are logically identical but hopefully you'll agree that the second version is much easier to debug, maintain and expand. I'm not even sure how to debug the first version. If you are repeating the same thing over and over and want to save the 3 extra lines of code, you can easily put the code into a function.

© Copyright 2018 HSU - All rights reserved.